home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tests / string.test < prev    next >
Text File  |  1993-02-06  |  11KB  |  334 lines

  1. # Commands covered:  string
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/string.test,v 1.7 93/02/06 15:54:24 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. test string-1.1 {string compare} {
  32.     string compare abcde abdef
  33. } -1
  34. test string-1.2 {string compare} {
  35.     string c abcde ABCDE
  36. } 1
  37. test string-1.3 {string compare} {
  38.     string compare abcde abcde
  39. } 0
  40. test string-1.4 {string compare} {
  41.     list [catch {string compare a} msg] $msg
  42. } {1 {wrong # args: should be "string compare string1 string2"}}
  43. test string-1.5 {string compare} {
  44.     list [catch {string compare a b c} msg] $msg
  45. } {1 {wrong # args: should be "string compare string1 string2"}}
  46.  
  47. test string-2.1 {string first} {
  48.     string first bq abcdefgbcefgbqrs
  49. } 12
  50. test string-2.2 {string first} {
  51.     string fir bcd abcdefgbcefgbqrs
  52. } 1
  53. test string-2.3 {string first} {
  54.     string f b abcdefgbcefgbqrs
  55. } 1
  56. test string-2.4 {string first} {
  57.     string first xxx x123xx345xxx789xxx012
  58. } 9
  59. test string-2.5 {string first} {
  60.     list [catch {string first a} msg] $msg
  61. } {1 {wrong # args: should be "string first string1 string2"}}
  62. test string-2.6 {string first} {
  63.     list [catch {string first a b c} msg] $msg
  64. } {1 {wrong # args: should be "string first string1 string2"}}
  65.  
  66. test string-3.1 {string index} {
  67.     string index abcde 0
  68. } a
  69. test string-3.2 {string index} {
  70.     string i abcde 4
  71. } e
  72. test string-3.3 {string index} {
  73.     string index abcde 5
  74. } {}
  75. test string-3.4 {string index} {
  76.     list [catch {string index abcde -10} msg] $msg
  77. } {0 {}}
  78. test string-3.5 {string index} {
  79.     list [catch {string index} msg] $msg
  80. } {1 {wrong # args: should be "string index string charIndex"}}
  81. test string-3.6 {string index} {
  82.     list [catch {string index a b c} msg] $msg
  83. } {1 {wrong # args: should be "string index string charIndex"}}
  84. test string-3.7 {string index} {
  85.     list [catch {string index a xyz} msg] $msg
  86. } {1 {expected integer but got "xyz"}}
  87.  
  88. test string-4.1 {string last} {
  89.     string la xxx xxxx123xx345x678
  90. } 1
  91. test string-4.2 {string last} {
  92.     string last xx xxxx123xx345x678
  93. } 7
  94. test string-4.3 {string last} {
  95.     string las x xxxx123xx345x678
  96. } 12
  97. test string-4.4 {string last} {
  98.     list [catch {string last a} msg] $msg
  99. } {1 {wrong # args: should be "string last string1 string2"}}
  100. test string-4.5 {string last} {
  101.     list [catch {string last a b c} msg] $msg
  102. } {1 {wrong # args: should be "string last string1 string2"}}
  103.  
  104. test string-5.1 {string length} {
  105.     string length "a little string"
  106. } 15
  107. test string-5.2 {string length} {
  108.     string le ""
  109. } 0
  110. test string-5.3 {string length} {
  111.     list [catch {string length} msg] $msg
  112. } {1 {wrong # args: should be "string length string"}}
  113. test string-5.4 {string length} {
  114.     list [catch {string length a b} msg] $msg
  115. } {1 {wrong # args: should be "string length string"}}
  116.  
  117. test string-6.1 {string match} {
  118.     string match abc abc
  119. } 1
  120. test string-6.2 {string match} {
  121.     string m abc abd
  122. } 0
  123. test string-6.3 {string match} {
  124.     string match ab*c abc
  125. } 1
  126. test string-6.4 {string match} {
  127.     string match ab**c abc
  128. } 1
  129. test string-6.5 {string match} {
  130.     string match ab* abcdef
  131. } 1
  132. test string-6.6 {string match} {
  133.     string match *c abc
  134. } 1
  135. test string-6.7 {string match} {
  136.     string match *3*6*9 0123456789
  137. } 1
  138. test string-6.8 {string match} {
  139.     string match *3*6*9 01234567890
  140. } 0
  141. test string-6.9 {string match} {
  142.     string match a?c abc
  143. } 1
  144. test string-6.10 {string match} {
  145.     string match a??c abc
  146. } 0
  147. test string-6.11 {string match} {
  148.     string match ?1??4???8? 0123456789
  149. } 1
  150. test string-6.12 {string match} {
  151.     string match {[abc]bc} abc
  152. } 1
  153. test string-6.13 {string match} {
  154.     string match {a[abc]c} abc
  155. } 1
  156. test string-6.14 {string match} {
  157.     string match {a[xyz]c} abc
  158. } 0
  159. test string-6.15 {string match} {
  160.     string match {12[2-7]45} 12345
  161. } 1
  162. test string-6.16 {string match} {
  163.     string match {12[ab2-4cd]45} 12345
  164. } 1
  165. test string-6.17 {string match} {
  166.     string match {12[ab2-4cd]45} 12b45
  167. } 1
  168. test string-6.18 {string match} {
  169.     string match {12[ab2-4cd]45} 12d45
  170. } 1
  171. test string-6.19 {string match} {
  172.     string match {12[ab2-4cd]45} 12145
  173. } 0
  174. test string-6.20 {string match} {
  175.     string match {12[ab2-4cd]45} 12545
  176. } 0
  177. test string-6.21 {string match} {
  178.     string match {a\*b} a*b
  179. } 1
  180. test string-6.22 {string match} {
  181.     string match {a\*b} ab
  182. } 0
  183. test string-6.23 {string match} {
  184.     string match {a\*\?\[\]\\\x} "a*?\[\]\\x"
  185. } 1
  186. test string-6.24 {string match} {
  187.     string match ** ""
  188. } 1
  189. test string-6.25 {string match} {
  190.     string match *. ""
  191. } 0
  192. test string-6.26 {string match} {
  193.     string match "" ""
  194. } 1
  195. test string-6.27 {string match} {
  196.     list [catch {string match a} msg] $msg
  197. } {1 {wrong # args: should be "string match pattern string"}}
  198. test string-6.28 {string match} {
  199.     list [catch {string match a b c} msg] $msg
  200. } {1 {wrong # args: should be "string match pattern string"}}
  201.  
  202. test string-7.1 {string range} {
  203.     string range abcdefghijklmnop 2 14
  204. } {cdefghijklmno}
  205. test string-7.2 {string range} {
  206.     string range abcdefghijklmnop 7 1000
  207. } {hijklmnop}
  208. test string-7.3 {string range} {
  209.     string range abcdefghijklmnop 10 e
  210. } {klmnop}
  211. test string-7.4 {string range} {
  212.     string range abcdefghijklmnop 10 9
  213. } {}
  214. test string-7.5 {string range} {
  215.     string range abcdefghijklmnop -3 2
  216. } {abc}
  217. test string-7.6 {string range} {
  218.     string range abcdefghijklmnop -3 -2
  219. } {}
  220. test string-7.7 {string range} {
  221.     string range abcdefghijklmnop 1000 1010
  222. } {}
  223. test string-7.8 {string range} {
  224.     string range abcdefghijklmnop -100 end
  225. } {abcdefghijklmnop}
  226. test string-7.9 {string range} {
  227.     list [catch {string range} msg] $msg
  228. } {1 {wrong # args: should be "string range string first last"}}
  229. test string-7.10 {string range} {
  230.     list [catch {string range a 1} msg] $msg
  231. } {1 {wrong # args: should be "string range string first last"}}
  232. test string-7.11 {string range} {
  233.     list [catch {string range a 1 2 3} msg] $msg
  234. } {1 {wrong # args: should be "string range string first last"}}
  235. test string-7.12 {string range} {
  236.     list [catch {string range abc abc 1} msg] $msg
  237. } {1 {expected integer but got "abc"}}
  238. test string-7.13 {string range} {
  239.     list [catch {string range abc 1 eof} msg] $msg
  240. } {1 {expected integer or "end" but got "eof"}}
  241.  
  242. test string-8.1 {string trim} {
  243.     string trim "    XYZ      "
  244. } {XYZ}
  245. test string-8.2 {string trim} {
  246.     string trim "\t\nXYZ\t\n\r\n"
  247. } {XYZ}
  248. test string-8.3 {string trim} {
  249.     string trim "  A XYZ A    "
  250. } {A XYZ A}
  251. test string-8.4 {string trim} {
  252.     string trim "XXYYZZABC XXYYZZ" ZYX
  253. } {ABC }
  254. test string-8.5 {string trim} {
  255.     string trim "    \t\r      "
  256. } {}
  257. test string-8.6 {string trim} {
  258.     string trim {abcdefg} {}
  259. } {abcdefg}
  260. test string-8.7 {string trim} {
  261.     string trim {}
  262. } {}
  263. test string-8.8 {string trim} {
  264.     string trim ABC DEF
  265. } {ABC}
  266. test string-8.9 {string trim} {
  267.     list [catch {string trim} msg] $msg
  268. } {1 {wrong # args: should be "string trim string ?chars?"}}
  269. test string-8.10 {string trim} {
  270.     list [catch {string trim a b c} msg] $msg
  271. } {1 {wrong # args: should be "string trim string ?chars?"}}
  272.  
  273. test string-9.1 {string trimleft} {
  274.     string trimleft "    XYZ      "
  275. } {XYZ      }
  276. test string-9.2 {string trimleft} {
  277.     list [catch {string triml} msg] $msg
  278. } {1 {wrong # args: should be "string trimleft string ?chars?"}}
  279.  
  280. test string-10.1 {string trimright} {
  281.     string trimright "    XYZ      "
  282. } {    XYZ}
  283. test string-10.2 {string trimright} {
  284.     string trimright "   "
  285. } {}
  286. test string-10.3 {string trimright} {
  287.     string trimright ""
  288. } {}
  289. test string-10.4 {string trimright errors} {
  290.     list [catch {string trimr} msg] $msg
  291. } {1 {wrong # args: should be "string trimright string ?chars?"}}
  292. test string-10.5 {string trimright errors} {
  293.     list [catch {string trimg a} msg] $msg
  294. } {1 {bad option "trimg": should be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, or trimright}}
  295.  
  296. test string-11.1 {string tolower} {
  297.     string tolower ABCDeF
  298. } {abcdef}
  299. test string-11.2 {string tolower} {
  300.     string tolower "ABC  XyZ"
  301. } {abc  xyz}
  302. test string-11.3 {string tolower} {
  303.     string tolower {123#$&*()}
  304. } {123#$&*()}
  305. test string-11.4 {string tolower} {
  306.     list [catch {string tolower} msg] $msg
  307. } {1 {wrong # args: should be "string tolower string"}}
  308. test string-11.5 {string tolower} {
  309.     list [catch {string tolower a b} msg] $msg
  310. } {1 {wrong # args: should be "string tolower string"}}
  311.  
  312. test string-12.1 {string toupper} {
  313.     string toupper abCDEf
  314. } {ABCDEF}
  315. test string-12.2 {string toupper} {
  316.     string toupper "abc xYz"
  317. } {ABC XYZ}
  318. test string-12.3 {string toupper} {
  319.     string toupper {123#$&*()}
  320. } {123#$&*()}
  321. test string-12.4 {string toupper} {
  322.     list [catch {string toupper} msg] $msg
  323. } {1 {wrong # args: should be "string toupper string"}}
  324. test string-12.5 {string toupper} {
  325.     list [catch {string toupper a b} msg] $msg
  326. } {1 {wrong # args: should be "string toupper string"}}
  327.  
  328. test string-13.1 {error conditions} {
  329.     list [catch {string gorp a b} msg] $msg
  330. } {1 {bad option "gorp": should be compare, first, index, last, length, match, range, tolower, toupper, trim, trimleft, or trimright}}
  331. test string-13.2 {error conditions} {
  332.     list [catch {string} msg] $msg
  333. } {1 {wrong # args: should be "string option arg ?arg ...?"}}
  334.